package com.ruoyi.web;


import io.minio.*;
import okhttp3.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class test {
    public static void main(String[] args) {
        try {
            System.out.println("当前系统时间: " + new Date());

            // 配置参数
            String endpoint = "https://oss.littlepeach.net";
            String accessKey = "minioadmin"; // 使用实际密钥
            String secretKey = "minioadmin"; // 使用实际密钥
            String bucketName = "smart";

            // 创建 HTTP 客户端
            OkHttpClient client = new OkHttpClient.Builder()
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .build();

            // 尝试直接上传文件（不依赖 MinIO SDK 进行签名）
            String content = "这是一个测试文件。上传时间: " + new Date();
            byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);

            String objectName = "direct-upload-" + System.currentTimeMillis() + ".txt";

            // 使用硬编码的日期进行签名（2024年而不是2025年）
            String amzDate = "20240330T070940Z"; // 注意这里改成了2024年

            // 计算内容 SHA256
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(contentBytes);
            String contentSha256 = bytesToHex(hash);

            // 构建请求
            Request.Builder requestBuilder = new Request.Builder()
                    .url(endpoint + "/" + bucketName + "/" + objectName)
                    .put(RequestBody.create(MediaType.parse("text/plain"), contentBytes));

            // 添加必要的标头
            requestBuilder.header("Host", new URL(endpoint).getHost());
            requestBuilder.header("x-amz-content-sha256", contentSha256);
            requestBuilder.header("x-amz-date", amzDate);

            // 执行上传请求
            Response response = client.newCall(requestBuilder.build()).execute();

            System.out.println("上传响应码: " + response.code());
            if (response.body() != null) {
                System.out.println("响应内容: " + response.body().string());
            }

            if (response.isSuccessful()) {
                System.out.println("文件上传成功: " + objectName);
            } else {
                System.out.println("文件上传失败");
            }

            // 尝试使用预签名URL方式
            System.out.println("\n尝试列出存储桶内容（不进行身份验证）...");
            Request listRequest = new Request.Builder()
                    .url(endpoint + "/" + bucketName + "?list-type=2&max-keys=10")
                    .get()
                    .build();

            Response listResponse = client.newCall(listRequest).execute();
            System.out.println("列表响应码: " + listResponse.code());

        } catch (Exception e) {
            System.out.println("发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuilder result = new StringBuilder();
        for (byte b : bytes) {
            result.append(String.format("%02x", b));
        }
        return result.toString();
    }
}
